<template>
  <div>
    <!-- 搜索框 -->
    <van-search
      shape="round"
      background="#c82519"
      placeholder="请输入搜索关键词"
      fixed
    />
    <!-- 商品列表 -->
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="getGoodsListData"
        class="goods-list"
      >
        <lite-virtual-list
          type="fixed"
          :size="105"
          :remain="10"
          :data="goodsList"
        >
          <div slot-scope="{ item }">
            <van-card
              class="goods-card"
              :title="item.proname"
              :thumb="item.img1"
            >
              <template #price>
                <span style="color: red; font-size: 15px">
                  ￥{{ item.originprice }}
                </span>
              </template>
              <template #num>
                <van-stepper
                  :value="item.number"
                  theme="round"
                  button-size="22"
                  :min="1"
                  disable-input
                />
              </template>
            </van-card>
          </div>
        </lite-virtual-list>

        <!-- <DynamicScroller
      :items="goodsList"
      :min-item-size="10"
      keyField="proid"
      class="scroller"
    >
      <template v-slot="{ item, index, active }">
        <DynamicScrollerItem
          :item="item"
          :active="active"
          :data-index="index"
          :key="item.proid"
        >
          <van-card class="goods-card" :title="item.proname" :thumb="item.img1">
            <template #price>
              <span style="color: red; font-size: 15px">
                ￥{{ item.originprice }}
              </span>
            </template>
            <template #num>
              <van-stepper
                :value="item.number"
                theme="round"
                button-size="22"
                :min="1"
                disable-input
              />
            </template>
          </van-card>
        </DynamicScrollerItem>
      </template>
    </DynamicScroller> -->
        <!-- <div
          class="goods-item"
          v-for="item in goodsList"
          :key="item.proid"
          @click="$router.push(`/detail?proid=${item.proid}`)"
        >
          <van-card class="goods-card" :title="item.proname" :thumb="item.img1">
            <template #price>
              <span style="color: red; font-size: 15px">
                ￥{{ item.originprice }}
              </span>
            </template>
            <template #num>
              <van-stepper
                :value="item.number"
                theme="round"
                button-size="22"
                :min="1"
                disable-input
              />
            </template>
          </van-card>
        </div> -->
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getGoodsList } from '@/api/home'
// import VirtualList from '@virtual-list/vue/vue2'
export default {
  data () {
    return {
      count: 0, // 页码
      // limitNum: 10 // 页容量
      goodsList: [],
      loading: false, // 加载状态
      finished: false, // 是否加载完毕
      isLoading: false // 是否正在下拉加载
    }
  },
  // components: {
  //   'v-list': VirtualList
  // },
  created () {
    this.getGoodsListData()
  },
  methods: {
    /**
     * 该方法第一进来要调用，上拉加载更多和下拉刷新都要调用
     */
    async getGoodsListData () {
      this.count++

      const res = await getGoodsList(this.count)

      this.goodsList = [...this.goodsList, ...res.data]

      if (res.data.length === 0) {
        // 没有数据啦
        this.finished = true
      }

      // 一定要把这个属性设置为false，否则滚动到下面就不会加载了
      this.loading = false
      // 数据加载完毕，把下刷刷新状态，设置为false
      this.isLoading = false
    },
    onRefresh () {
      // 把之前的数据全清空
      this.goodsList = []
      // 重新加载第一页的数据
      this.count = 0
      this.getGoodsListData()
    }
  }
}
</script>

<style lang="scss" scoped>
.scroller {
  height: 100%;
}
.goods-card {
  background-color: white;
  border-bottom: 1px solid #f0f0f0;
}
</style>
